using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DALTMClient.WSDL;

namespace DALTMClient
{
    public partial class frmReceiveMessOffline : Form
    {
        DataTable dt = new DataTable();
        AluMessenger chat = new AluMessenger();
        public frmReceiveMessOffline()
        {
            InitializeComponent();
        }

        private void frmReceiveMessOffline_Load(object sender, EventArgs e)
        {
            try
            {
                rtfViewMess.SelectionIndent = 10;
                rtfViewMess.SelectionRightIndent = 10;
                dt = chat.ReceiveMessageOffline(frmAluMain.UserName);
                ListViewItem item;
                string mess = "";
                foreach (DataRow row in dt.Rows)
                {
                    mess = DESClass.Decrypt(row["Mess"].ToString());
                    item = new ListViewItem(row["Username"].ToString(), 0);
                    item.SubItems.Add(mess);
                    item.SubItems.Add(row["MessDateTime"].ToString());
                    item.SubItems.Add(row["ToUser"].ToString());

                    lvwMessOffline.Items.Add(item);
                }
                chat.RemoveMessageOffline(frmAluMain.UserName);
            }
            catch            {            }
        }

        private void frmReceiveMessOffline_MaximumSizeChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvwMessOffline.Columns.Count; i++)
            {
                lvwMessOffline.Columns[i].Width = lvwMessOffline.Size.Width / 4;
            }
        }

        private void frmReceiveMessOffline_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < lvwMessOffline.Columns.Count; i++)
            {
                lvwMessOffline.Columns[i].Width = lvwMessOffline.Size.Width / 4;
            }
        }

        private void btnReply_Click(object sender, EventArgs e)
        {
            if (lvwMessOffline.FocusedItem != null)
            {
                frmAluMain.TitleFormMess = String.Format("{0} send message to {1}", frmAluMain.UserName, lvwMessOffline.FocusedItem.SubItems[0].Text);
                frmAluMain.ListChat.Add(lvwMessOffline.FocusedItem.SubItems[0].Text);
                frmSendMessenger frm = new frmSendMessenger();
                frm.Show();
            }
        }

        private void lvwMessOffline_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            rtfViewMess.Text = String.Format("{0}: {1}", e.Item.SubItems[0].Text, e.Item.SubItems[1].Text);
            e.Item.Font = rtfViewMess.Font;
            btnReply.Enabled = true;
            btnDelete.Enabled = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(lvwMessOffline.FocusedItem!=null)
            lvwMessOffline.Items.RemoveAt(lvwMessOffline.FocusedItem.Index);
            rtfViewMess.Text="";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you want close sure ?", "Alu! Messenger", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                Close();
        }
    }
}